/**
 * 修改时间：2009-01-05
 * 修改人：陈大国
 * 修改原因：一个服务厅可同时对应两个供货平台（一个2G手机供货平台和一个TD手机供货平台）
 * 
 * 修改时间：2009-12-01
 * 修改人：朱家进
 * 修改内容：增加验证市级连锁是否只有一个市仓
 * 
 * 修改时间：2010-05-18
 * 修改人：朱家进
 * 修改内容：增加入柜商对应的自有渠道厅关系 
 */

package com.zjht.gmcc.servlet.sys;

import java.io.PrintWriter;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.zjht.gmcc.bean.BaseServlet;

import com.zjht.gmcc.bean.util.InitParam;
import com.zjht.gmcc.bean.util.MyConstant;
import com.zjht.gmcc.bean.util.MyJDBC;

public class OrgaAdd extends BaseServlet {

	private static final long serialVersionUID = 1L;

	public boolean process(HttpServletRequest request, HttpServletResponse response, HttpSession session, PrintWriter out, MyJDBC myJDBC, String menu)
			throws Exception {

		String s_porgatype = request.getParameter("porgaType");// 父组织类型
		String s_porgacode = request.getParameter("porgacode");// 父组织编码

		String s_orgacode = request.getParameter("orgacode").trim().toUpperCase();// 新添加组织编码
		String s_organame = request.getParameter("organame");// 新添加组织名称

		String s_kind = request.getParameter("kind");// 标志位，添加省级组织 == 1 否则为添加下级组织
		String s_actorcode = request.getParameter("actorcode");// 新添 例:W
		String channelType = request.getParameter("channelType");// 渠道类型

		int i_flag = 0;
		PreparedStatement ps = null;

		String s_shortname = request.getParameter("shortname");
		String s_linkman = request.getParameter("linkman");
		String s_postcode = request.getParameter("postcode");
		String s_fax = request.getParameter("fax");
		String s_tel = request.getParameter("tel");
		String s_address = request.getParameter("address");
		String s_areaid = request.getParameter("areaid");
		String s_status = (request.getParameter("status") == null) ? "0" : request.getParameter("status");
		

		if (s_porgatype == null || s_porgatype.length() == 0) {
			alert(out, "无法更新组织信息，请与管理员联系。");
			return false;
		}

		String s_sql = "";
		ResultSet rs = null;
		s_sql = "SELECT orgid FROM organiza WHERE orgacode = ? ";
		ps = myJDBC.prepareStatement(s_sql);
		ps.setString(1, s_porgacode);
		rs = ps.executeQuery();
		String parentorgid = null;
		if (rs.next()) {
			parentorgid = rs.getString("orgid");
		}
		// 验证市级连锁是否只有一个市仓
		if (s_porgatype.equals("5") && channelType.equals(MyConstant.CHANNEL_SOCIAL_CITY)) {
			s_sql = "SELECT * FROM organiza WHERE parentorgid = ? AND channeltype = ? ";
			ps = myJDBC.prepareStatement(s_sql);
			ps.setString(1, parentorgid);
			ps.setString(2, MyConstant.CHANNEL_SOCIAL_CITY);
			rs = ps.executeQuery();
			if (rs.next()) {
				alertOK(out, "市级社会渠道只能有一个市仓！\\n\\n请确认后再添加！", "window.close();");
				return false;
			}
		}

		if (s_kind != null)// 添加省级组织
		{
			s_sql = "INSERT INTO organiza(orgacode, organame, parentorgacode, orgatypeid, actorcode, "
					+ "areaid, address, postcode, linkman, telephone, fax, createdate, "
					+ "shortname, paraid, status ,fillhour,channeltype,orgid,parentorgid) "
					+ "VALUES(?,?,0,?,?,?,?,?,?,?,?,sysdate,?,0,?,null,?,seq_orgid.nextval,-1)";
			ps = myJDBC.prepareStatement(s_sql);
			ps.setString(1, s_orgacode);
			ps.setString(2, s_organame);
			ps.setString(3, s_porgatype);
			ps.setString(4, s_actorcode);
			ps.setString(5, s_areaid);
			ps.setString(6, s_address);
			ps.setString(7, s_postcode);
			ps.setString(8, s_linkman);
			ps.setString(9, s_tel);
			ps.setString(10, s_fax);
			ps.setString(11, s_shortname);
			ps.setString(12, s_status);
			ps.setString(13, channelType);

			i_flag = ps.executeUpdate();
			ps.close();
		} else if (s_porgatype.equals("81") || s_porgatype.equals("5") || s_porgatype.equals("2") || s_porgatype.equals("6"))
		// 如果添加市平台或市社会渠道或区/县移动
		{
			String tempOrgaCode = null;
			if (s_porgatype.equals("6")) {
				s_sql = "INSERT INTO organiza(orgid,orgacode,organame,orgatypeid,parentorgid,actorcode,areaid,address,postcode,linkman,telephone,fax,createdate,stopdate,shortname,paraid,status,appointreceiver,appointaddress,appointtel,orderid,sdate,edate,fillhour,channeltype,multiplat,parentorgacode) "
						+ " SELECT seq_orgid.nextval,?,?,3,orgid,actorcode,?,?,?,?,?,?,SYSDATE,null,? ,"
						+ "null,?,null,null,null,null,null,null,null,?,multiplat,orgacode " + " FROM organiza where orgacode = ?";
				tempOrgaCode = s_porgacode + request.getParameter("orgacodehall");
			} else {
				s_sql = "INSERT INTO organiza(orgid,orgacode,organame,orgatypeid,parentorgid,actorcode,areaid,address,postcode,linkman,telephone,fax,createdate,stopdate,shortname,paraid,status,appointreceiver,appointaddress,appointtel,orderid,sdate,edate,fillhour,channeltype,multiplat,parentorgacode)  "
						+ " SELECT seq_orgid.nextval,?,?,orgatypeid+1,orgid,actorcode,?,?,?,?,?,?,SYSDATE,null,? ,"
						+ "null,?,null,null,null,null,null,null,null,?,multiplat,orgacode " + " FROM organiza where orgacode = ?";
				tempOrgaCode = s_orgacode;
			}
			ps = myJDBC.prepareStatement(s_sql);
			ps.setString(1, tempOrgaCode);
			ps.setString(2, s_organame);
			ps.setString(3, s_areaid);
			ps.setString(4, s_address);
			ps.setString(5, s_postcode);
			ps.setString(6, s_linkman);
			ps.setString(7, s_tel);
			ps.setString(8, s_fax);
			ps.setString(9, s_shortname);
			ps.setString(10, s_status);
			ps.setString(11, channelType);
			ps.setString(12, s_porgacode);

			i_flag = ps.executeUpdate();
			ps.close();
		} else if (s_porgatype.equals("60"))// 添加售后服务站
		{
			s_orgacode = s_porgacode + request.getParameter("orgacodehall");
			s_sql = "INSERT INTO organiza(orgacode, organame, parentorgacode, orgatypeid, actorcode, "
					+ "areaid, address, postcode, linkman, telephone, fax, createdate, "
					+ "shortname, paraid, status ,fillhour,channeltype,orgid,parentorgid) "
					+ "VALUES(?,?,?,?,?,?,?,?,?,?,?,sysdate,?,0,?,null,?,seq_orgid.nextval,?)";
			ps = myJDBC.prepareStatement(s_sql);
			ps.setString(1, s_orgacode);
			ps.setString(2, s_organame);
			ps.setString(3, s_porgacode);
			ps.setString(4, "61");
			ps.setString(5, "S");
			ps.setString(6, s_areaid);
			ps.setString(7, s_address);
			ps.setString(8, s_postcode);
			ps.setString(9, s_linkman);
			ps.setString(10, s_tel);
			ps.setString(11, s_fax);
			ps.setString(12, s_shortname);
			ps.setString(13, s_status);
			ps.setString(14, channelType);
			ps.setString(15, parentorgid);

			i_flag = ps.executeUpdate();
			ps.close();
		} else if (s_porgatype.equals("3") || s_porgatype.equals("82"))// 如果是添加服务厅或社会渠道厅，则需要更新营业时间等信息
		{
			String s_halltypeid = request.getParameter("halltype");
			String s_fillhour = request.getParameter("fillhour");
			String s_sdate1 = request.getParameter("sdate1");
			String s_sdate2 = request.getParameter("sdate2");
			String s_edate1 = request.getParameter("edate1");
			String s_edate2 = request.getParameter("edate2");
			String autoFlag = request.getParameter("autoFlag");
			
			if(MyConstant.CHANNEL_GMCC.equals(channelType)){//自有渠道服务厅
				s_sql = "INSERT INTO organiza(orgid,orgacode,organame,orgatypeid,parentorgid,actorcode,areaid,address,postcode,linkman,telephone,fax,createdate,stopdate,shortname,paraid,status,appointreceiver,appointaddress,appointtel,orderid,sdate,edate,fillhour,channeltype,multiplat,parentorgacode,halllevel,halltype) "
					+ " SELECT seq_orgid.nextval,?,?,orgatypeid+1,orgid,actorcode,?,?,?,?,?,?,SYSDATE,null,? ,"
					+ "?,?,null,null,null,null,?,?,?,?,multiplat,orgacode,?,? " + " FROM organiza where orgacode = ?";
				ps = myJDBC.prepareStatement(s_sql);
				ps.setString(1, s_orgacode);
				ps.setString(2, s_organame);
				ps.setString(3, s_areaid);
				ps.setString(4, s_address);
				ps.setString(5, s_postcode);
				ps.setString(6, s_linkman);
				ps.setString(7, s_tel);
				ps.setString(8, s_fax);
				ps.setString(9, s_shortname);
				ps.setString(10, "45");//以前的服务厅类型
				ps.setString(11, s_status);
				ps.setString(12, s_sdate1 + ":" + s_sdate2);
				ps.setString(13, s_edate1 + ":" + s_edate2);
				ps.setString(14, s_fillhour);
				ps.setString(15, channelType);
				ps.setInt(16, 1);
				ps.setString(17, s_halltypeid);
				ps.setString(18, s_porgacode);
				
				i_flag = ps.executeUpdate();
				ps.close();
			}else{
				s_sql = "INSERT INTO organiza(orgid,orgacode,organame,orgatypeid,parentorgid,actorcode,areaid,address,postcode,linkman,telephone,fax,createdate,stopdate,shortname,paraid,status,appointreceiver,appointaddress,appointtel,orderid,sdate,edate,fillhour,channeltype,multiplat,parentorgacode) "
					+ " SELECT seq_orgid.nextval,?,?,orgatypeid+1,orgid,actorcode,?,?,?,?,?,?,SYSDATE,null,? ,"
					+ "?,?,null,null,null,null,?,?,?,?,multiplat,orgacode " + " FROM organiza where orgacode = ?";
				ps = myJDBC.prepareStatement(s_sql);
				ps.setString(1, s_orgacode);
				ps.setString(2, s_organame);
				ps.setString(3, s_areaid);
				ps.setString(4, s_address);
				ps.setString(5, s_postcode);
				ps.setString(6, s_linkman);
				ps.setString(7, s_tel);
				ps.setString(8, s_fax);
				ps.setString(9, s_shortname);
				ps.setString(10, s_halltypeid);
				ps.setString(11, s_status);
				ps.setString(12, s_sdate1 + ":" + s_sdate2);
				ps.setString(13, s_edate1 + ":" + s_edate2);
				ps.setString(14, s_fillhour);
				ps.setString(15, channelType);
				ps.setString(16, s_porgacode);
				i_flag = ps.executeUpdate();
				ps.close();
			}
			
			if(i_flag == 1 && MyConstant.CHANNEL_GMCC.equals(channelType)){//自有渠道才更新自动补货autoflag字段
				s_sql = "UPDATE organiza SET autoflag = ? WHERE orgacode = ?";
				ps = myJDBC.prepareStatement(s_sql);
				ps.setString(1, autoFlag);
				ps.setString(2, s_orgacode);
				i_flag = ps.executeUpdate();
				ps.close();				
			}
			//自有渠道
			//当服务厅2G或3G平台其中有一个选择为“终端公司”时，organiza表DDflag必须设置为'1'；如果未选择终端公司，必须设置为'0'
			if(i_flag == 1 && MyConstant.CHANNEL_GMCC.equals(channelType)){
				String ddFlag = "0";
				String provider = request.getParameter("provider");
				String providerTD = request.getParameter("providerTD");
				if("ZD".equals(provider.substring(0,provider.indexOf(",")))){
					ddFlag = "1";
				}
				if(null != providerTD && !"0".equals(providerTD)){
					if("ZD".equals(providerTD.substring(0,providerTD.indexOf(",")))){
						ddFlag = "1";
					}
				}
				s_sql = "UPDATE organiza SET ddflag = ? WHERE orgacode = ?";
				ps = myJDBC.prepareStatement(s_sql);
				ps.setString(1, ddFlag);
				ps.setString(2, s_orgacode);
				i_flag = ps.executeUpdate();
				ps.close();						
			}
			

			if (i_flag == 1 && !channelType.equals(MyConstant.CHANNEL_FD)) {
				i_flag = 0;
				String s_provider = request.getParameter("provider");
				String s_providerTD = request.getParameter("providerTD");// CDG2009-01-05TD手机供货商
				// CDG2009-01-05
				if ("0".equals(s_providerTD)) {// 没有选择TD手机供货商
					if (s_provider != null && s_provider.length() > 0 && s_provider.indexOf(",") != -1) {
						s_sql = "insert into MOBILETOPROVIDER(mobileorgacode, providerorgacode, cityproviderorgacode, status, "
								+ "ValidDate, CityProviderType) " + "VALUES(?, ?, ?, 1, Sysdate, '2')";
						ps = myJDBC.prepareStatement(s_sql);
						ps.setString(1, s_orgacode);
						ps.setString(2, s_provider.substring(0, s_provider.indexOf(",")));
						ps.setString(3, s_provider.substring(s_provider.indexOf(",") + 1, s_provider.length()));
						i_flag = ps.executeUpdate();
						ps.close();
					}
				} else {// 选择TD手机供货商
					if (s_providerTD != null && s_providerTD.equals(s_provider)) {// 2G手机和TD手机是同一个供货商
						if (s_provider != null && s_provider.length() > 0 && s_provider.indexOf(",") != -1) {
							s_sql = "insert into MOBILETOPROVIDER(mobileorgacode, providerorgacode, cityproviderorgacode, status, "
									+ "ValidDate, CityProviderType) " + "VALUES(?, ?, ?, 1, Sysdate, '1')";
							ps = myJDBC.prepareStatement(s_sql);
							ps.setString(1, s_orgacode);
							ps.setString(2, s_provider.substring(0, s_provider.indexOf(",")));
							ps.setString(3, s_provider.substring(s_provider.indexOf(",") + 1, s_provider.length()));
							i_flag = ps.executeUpdate();
							ps.close();
							ps = null;
						}
					} else {// 2G手机和TD手机不是同一个供货商
						// 插入2G手机供货商
						if (s_provider != null && s_provider.length() > 0 && s_provider.indexOf(",") != -1) {
							s_sql = "insert into MOBILETOPROVIDER(mobileorgacode, providerorgacode, cityproviderorgacode, status, "
									+ "ValidDate, CityProviderType) " + "VALUES(?, ?, ?, 1, Sysdate, '2')";
							ps = myJDBC.prepareStatement(s_sql);
							ps.setString(1, s_orgacode);
							ps.setString(2, s_provider.substring(0, s_provider.indexOf(",")));
							ps.setString(3, s_provider.substring(s_provider.indexOf(",") + 1, s_provider.length()));
							i_flag = ps.executeUpdate();
							ps.close();
							ps = null;
						}
						// 插入TD手机供货商
						if (i_flag == 1 && s_providerTD != null && s_providerTD.length() > 0 && s_providerTD.indexOf(",") != -1) {
							s_sql = "insert into MOBILETOPROVIDER(mobileorgacode, providerorgacode, cityproviderorgacode, status, "
									+ "ValidDate, CityProviderType) " + "VALUES(?, ?, ?, 1, Sysdate, '3')";
							ps = myJDBC.prepareStatement(s_sql);
							ps.setString(1, s_orgacode);
							ps.setString(2, s_providerTD.substring(0, s_providerTD.indexOf(",")));
							ps.setString(3, s_providerTD.substring(s_providerTD.indexOf(",") + 1, s_providerTD.length()));
							i_flag = ps.executeUpdate();
							ps.close();
							ps = null;
						}
					}
				}
			}
		}
		if (i_flag == 1 && (s_porgatype.equals("3") || s_porgatype.equals("82") || s_porgatype.equals("2")))// 更新BOSS渠道标识
		{
			String s_mobileorgacode = request.getParameter("mobileorgacode");
			if (s_mobileorgacode != null && s_mobileorgacode.length() > 0) {
				s_sql = "INSERT INTO mobileorgatolocal(mobileorgacode,orgacode)VALUES(?,?)";
				ps = myJDBC.prepareStatement(s_sql);
				ps.setString(1, s_mobileorgacode);
				ps.setString(2, s_orgacode);
				i_flag = ps.executeUpdate();
				ps.close();
			}
		}
		if (i_flag == 1 && s_porgatype.equals("5") && channelType.equals(MyConstant.CHANNEL_FD))// 当新增FD市平台时，则把本市下所有厅对应关系加到mobiletoprovider
		{
			s_sql = "SELECT a.orgacode FROM organiza a " + "WHERE a.areaid = ? AND a.orgatypeid = 4 AND a.status = '1' AND a.channeltype = ?";
			ps = myJDBC.prepareStatement(s_sql);
			ps.setString(1, s_areaid);
			ps.setString(2, MyConstant.CHANNEL_FD);
			rs = ps.executeQuery();

			s_sql = "INSERT INTO mobiletoprovider(mobileorgacode, providerorgacode, cityproviderorgacode, status, "
					+ "ValidDate, CityProviderType,multiplat) " + "VALUES(?, ?, ?, 1, Sysdate, '1','1')";
			ps = myJDBC.prepareStatement(s_sql);

			while (rs.next()) {
				ps.setString(1, rs.getString("orgacode"));
				ps.setString(2, s_porgacode);
				ps.setString(3, s_orgacode);
				i_flag = ps.executeUpdate();
				if (i_flag == 0)
					break;
			}
			ps.close();
			ps = null;
		}
		if (i_flag == 1 && s_porgatype.equals("3"))// 如果添加服务厅，则需要添加服务厅库存最高限额
		{
			/*String s_ssprice = request.getParameter("ssprice");
			if (s_ssprice != null && s_ssprice.length() > 0) {
				s_sql = "INSERT INTO SS_PRICE (ID,OrgaCode,Price,CreateDate,CreateOperCode) VALUES (SS_PriceID.NEXTVAL,?,?,SYSDATE,?)";
				ps = myJDBC.prepareStatement(s_sql);
				ps.setString(1, s_orgacode);
				ps.setString(2, s_ssprice);
				ps.setString(3, MySession.getUsercode(session));
				i_flag = ps.executeUpdate();
				ps.close();
			}*/

			if (i_flag == 1 && !channelType.equals(MyConstant.CHANNEL_FD))// 当新增服务厅时，如本市有a.multiplat = '1'平台，则把对应关系加到mobiletoprovider
			{
				s_sql = "SELECT a.orgacode,a.parentorgacode FROM organiza a "
						+ "WHERE a.multiplat = '1' AND a.areaid = ? AND a.orgatypeid = 6 AND a.actorcode = 'P' AND a.status = '1'";
				ps = myJDBC.prepareStatement(s_sql);
				ps.setString(1, s_areaid);
				rs = ps.executeQuery();

				s_sql = "INSERT INTO mobiletoprovider(mobileorgacode, providerorgacode, cityproviderorgacode, status, "
						+ "ValidDate, CityProviderType,multiplat) " + "VALUES(?, ?, ?, 1, Sysdate, '1','1')";
				ps = myJDBC.prepareStatement(s_sql);

				while (rs.next()) {
					ps.setString(1, s_orgacode);
					ps.setString(2, rs.getString("parentorgacode"));
					ps.setString(3, rs.getString("orgacode"));
					i_flag = ps.executeUpdate();
					if (i_flag == 0)
						break;
				}
				ps.close();
				ps = null;
			}
			if (i_flag == 1 && channelType.equals(MyConstant.CHANNEL_FD))// 当新增服务厅时(FD渠道类型)，把本市所有的FD市平台跟此厅关联
			{
				s_sql = "SELECT a.orgacode,a.parentorgacode FROM organiza a "
						+ "WHERE a.areaid = ? AND a.orgatypeid = 6 AND a.actorcode = 'P' AND a.status = '1' AND a.channeltype = '07'";
				ps = myJDBC.prepareStatement(s_sql);
				ps.setString(1, s_areaid);
				rs = ps.executeQuery();

				s_sql = "INSERT INTO mobiletoprovider(mobileorgacode, providerorgacode, cityproviderorgacode, status, "
						+ "ValidDate, CityProviderType,multiplat) " + "VALUES(?, ?, ?, 1, Sysdate, '1','1')";
				ps = myJDBC.prepareStatement(s_sql);

				while (rs.next()) {
					ps.setString(1, s_orgacode);
					ps.setString(2, rs.getString("parentorgacode"));
					ps.setString(3, rs.getString("orgacode"));
					i_flag = ps.executeUpdate();
					if (i_flag == 0)
						break;
				}
				ps.close();
				ps = null;
			}
		}
		if (i_flag == 1 && s_porgatype.equals("3") && channelType.equals(MyConstant.CHANNEL_RGS))// 如果添加入柜商服务厅，则需要添加服务厅对应的自有渠道厅
		{
			String gmccHall = request.getParameter("gmccHall");// 入柜商对应的自有渠道厅
			if (gmccHall != null && gmccHall.length() > 0) {
				s_sql = "INSERT INTO mobiletorgs (mobilecode,rgscode) VALUES (?,?)";
				ps = myJDBC.prepareStatement(s_sql);
				ps.setString(1, gmccHall);
				ps.setString(2, s_orgacode);
				i_flag = ps.executeUpdate();
				ps.close();
			}
		}
		String act = s_organame + " 添加";
		if (i_flag > 0) {
			InitParam.initOrgaInfo();
			if (s_kind != null)// 添加省级组织
			{
				alertOK(out, act + "成功！", "dialogArguments.parent.orgaleftFrame.location.reload();window.close();");
			} else {
				alertOK(out, act + "成功！", "dialogArguments.location.href='orga/orga.jsp?orgacode=" + s_orgacode + "&channelType=" + channelType
						+ "&orgaType=" + (Integer.parseInt(s_porgatype) + 1) + "';window.returnValue='" + s_orgacode + "';window.close();");
			}

		} else {
			errAlert(request, out, menu, act + "失败");
		}
		return i_flag > 0;

	}
}
